import { Injectable } from '@angular/core';

/** 文件操作 */
@Injectable({ providedIn: 'root' })
export class FileService {
  /** 在新窗口中打开 */
  openInTab(blob: Blob): void {
    const url = window.URL.createObjectURL(blob);
    window.open(url, '_blank');
  }

  /** 保存文件 */
  saveAsFile(blob: Blob, fileName: string): void {
    const link = document.createElement('a');
    link.setAttribute('href', window.URL.createObjectURL(blob));
    link.setAttribute('download', fileName);
    link.style.visibility = 'hidden';
    document.body.appendChild(link);
    link.click();
    document.body.removeChild(link);
  }
}
